// connection.js
const mysql = require('mysql');

// 创建数据库连接
const connection = mysql.createConnection({
    host: '117.78.4.177',
    user: 'chat_app',
    password: '001198293',
    database: 'chat_app'
});

// 连接数据库
connection.connect((err) => {
    if (err) {
        console.error('Error connecting to database:', err);
        return;
    }
    console.log('数据库链接成功');
});

/**
 * 
 * @param {sql命令} query 
 * @returns Promise
 */
function executiveCommand(query) {
    console.log(query);
    return new Promise((re, rj) => {
        connection.query(query, (err, results) => {
            if (err) {
                return rj(err)
            }
            re(results)
        });

    })
}

module.exports = executiveCommand;